import request from '@/utils/request'
import { compare, toDecimal } from '@/utils/index'
const nameList = [
    {"parkName": "上海漕河泾新兴技术开发区", "shortName": "漕河泾开发区"},
    {"parkName": "上海漕河泾浦江国际科技城", "shortName": "浦江国际科技城"},
    {"parkName": "上海漕河泾开发区松江高科技园", "shortName": "松江高科技园"},
    {"parkName": "上海漕河泾开发区新经济园(漕河路)", "shortName": "新经济园(漕河路)"},
    {"parkName": "临港产业区", "shortName": "临港产业区"},
    {"parkName": "临港奉贤园区", "shortName": "临港奉贤园区"},
    {"parkName": "临港科技城", "shortName": "临港科技城"},
    {"parkName": "漕河泾开发区南桥园区", "shortName": "南桥园区"},
    {"parkName": "漕河泾南桥欣创园", "shortName": "南桥欣创园"},
]

function getShortName(parkName) {
    const item = nameList.find(item => item.parkName === parkName)
    return (item && item.shortName) || '无'
}


function getData({ year, key } = {}) {
    const obj = require(`@/static/js/${year}-park`)
    return JSON.parse(JSON.stringify(obj[key]))
}

// 省份数据
export function getProvinceJson(data = [], jsonName = '') {
    return request({
        url: `./static/json/province/${jsonName}.json`,
        method: 'get',
        data
    })
}

// 全部生产企业
export function getEnterpriseCount( { year, parkName } = {} ) {
    const list = getData({ year, key: 'enterpriseNum' })
    const data = parkName ? list.filter(item => item.parkName === parkName) : list
    let ivdCount = 0, activeCount = 0, passiveCount = 0
    data.forEach(item => {
        ivdCount += item['ivd']
        activeCount += item['active']
        passiveCount += item['passive']
    })
    return [
        {
            type: 'IVD',
            counts: ivdCount
        },
        {
            type: '有源',
            counts: activeCount
        },
        {
            type: '无源',
            counts: passiveCount
        }
    ]
}

// 注册产品类型
export function getProductTypeCount( { year, parkName } = {} ) {
    const list = getData({ year, key: 'productNum' })
    const data = parkName ? list.filter(item => item.parkName === parkName) : list
    let ivdCount = 0, activeCount = 0, passiveCount = 0
    data.forEach(item => {
        ivdCount += item['ivd']
        activeCount += item['active']
        passiveCount += item['passive']
    })
    return [
        {
            type: 'IVD',
            counts: ivdCount
        },
        {
            type: '有源',
            counts: activeCount
        },
        {
            type: '无源',
            counts: passiveCount
        }
    ]
}

// 注册产品分类数量
export function getProductClassifyCount( { year, parkName } = {} ) {
    const list = getData({ year, key: 'productNum' })
    const data = parkName ? list.filter(item => item.parkName === parkName) : list
    let oneCount = 0, twoCount = 0, threeCount = 0
    data.forEach(item => {
        oneCount += item['1']
        twoCount += item['2']
        threeCount += item['3']
    })
    return [
        {
            type: 'I类',
            counts: oneCount
        },
        {
            type: 'II类',
            counts: twoCount
        },
        {
            type: 'III类',
            counts: threeCount
        }
    ]
}

// 生产企业排名
export function getEnterpriseRanking( { year } = {} ) {
    const list = getData({ year, key: 'enterpriseNum' })
    const data = list.map(item => {
        return {
            type: getShortName(item['parkName']),
            counts: item['ivd'] + item['active'] + item['passive']
        }
    })
    return data.filter(item => item.counts > 0).sort(compare('counts', 'desc'))
}

// 注册产品排名
export function getProductRanking( { year } = {} ) {
    const list = getData({ year, key: 'productNum' })
    const data = list.map(item => {
        return {
            type: getShortName(item['parkName']),
            counts: item['1'] + item['2'] + item['3']
        }
    })
    return data.filter(item => item.counts > 0).sort(compare('counts'))
}

// 资讯列表
export function getInfoList() {
    const { infoList } = require(`@/static/js/info`)
    return infoList
}

// 企业名单
export function getEnterpriseList({ year, parkName } = {}) {
    const list = getData({ year, key: 'enterpriseList' })
    const data = parkName ? list.filter(item => item.parkName === parkName || item.themePark === parkName) : list
    return data.sort(compare('counts', 'desc'))
}

// 产品领域
export function getProductFiled({ year, parkName } = {}) {
    const list = getData({ year, key: 'productField' })
    const park = list.find(item => item.parkName === parkName)
    let data = []
    if (park !== undefined) {
        delete park['parkName']
        data = Object.keys(park).map(item => {
            return {
                type: item,
                counts: park[item]
            }
        })
    }
    return data.filter(item => item.counts > 0).sort(compare('counts', 'desc')).slice(0, 10)
}

// 全部生产企业
export function getEnterpriseTotal( { year } = {} ) {
    const list = getData({ year, key: 'enterpriseNum' })
    let total = 0
    list.forEach(item => {
        // total = item['1'] + item['2'] + item['3']
        Object.keys(item).forEach(i => {
            if (typeof (item[i]) === 'number') {
                total += item[i]
            }
        })
    })
    const obj = getData({ year, key: 'shanghaiEnterpriseNum' })
    let cityTotal = 0
    Object.keys(obj).forEach(i => {
        if (typeof (obj[i]) === 'number') {
            cityTotal += obj[i]
        }
    })
    return {
        total,
        percentage: toDecimal((total / cityTotal * 100)) + '%'
    }
}

// 全部注册产品
export function getProductTotal( { year } = {} ) {
    const list = getData({ year, key: 'productNum' })
    let total = 0
    list.forEach(item => {
        total = total + item['1'] + item['2'] + item['3']
        // Object.keys(item).forEach(i => {
        //     if (typeof (item[i]) === 'number') {
        //         total += item[i]
        //     }
        // })
    })
    const obj = getData({ year, key: 'shanghaiProductNum' })
    let cityTotal = obj['1'] + obj['2'] + obj['3']
    // Object.keys(obj).forEach(i => {
    //     if (typeof (obj[i]) === 'number') {
    //         cityTotal += obj[i]
    //     }
    // })
    return {
        total,
        percentage: toDecimal((total / cityTotal * 100)) + '%'
    }
}

// 园区生产企业
export function getEnterprisePark( { year, parkName } = {} ) {
    const list = getData({ year, key: 'enterpriseNum' })
    const obj = list.find(item => item.parkName === parkName)
    let parkTotal = 0
    Object.keys(obj).forEach(i => {
        if (typeof (obj[i]) === 'number') {
            parkTotal += obj[i]
        }
    })

    let total = 0
    list.forEach(item => {
        Object.keys(item).forEach(i => {
            if (typeof (item[i]) === 'number') {
                total += item[i]
            }
        })
    })
    return {
        parkTotal,
        percentage: toDecimal((parkTotal / total * 100)) + '%'
    }
}

// 园区注册产品
export function getProductPark( { year, parkName } = {} ) {
    const list = getData({ year, key: 'productNum' })
    const obj = list.find(item => item.parkName === parkName)
    const parkTotal = obj['1'] + obj['2'] + obj['3']
    // let parkTotal = 0
    // Object.keys(obj).forEach(i => {
    //     if (typeof (obj[i]) === 'number') {
    //         parkTotal += obj[i]
    //     }
    // })
    let total = 0
    list.forEach(item => {
        total = total + item['1'] + item['2'] + item['3']
        // Object.keys(item).forEach(i => {
        //     total = total + item['1'] + item['2'] + item['3']
        //     if (typeof (item[i]) === 'number') {
        //         total += item[i]
        //     }
        // })
    })
    return {
        parkTotal,
        percentage: toDecimal((parkTotal / total * 100)) + '%'
    }
}

// 获取园区企业及产品数量
export function getParkNum( { year } = {} ) {
    const enterpriseList = getData({ year, key: 'enterpriseNum' })
    const productList = getData({ year, key: 'productNum' })
    return enterpriseList.map(enterpriseInfo => {
        const productInfo = productList.find(item => item.parkName === enterpriseInfo.parkName)
        let enterpriseNum = 0, productNum = productInfo['1'] + productInfo['2'] + productInfo['3']
        Object.keys(enterpriseInfo).forEach(i => {
            if (typeof (enterpriseInfo[i]) === 'number') {
                enterpriseNum += enterpriseInfo[i]
            }
        })
        // Object.keys(productInfo).forEach(i => {
        //     if (typeof (productInfo[i]) === 'number') {
        //         productNum += productInfo[i]
        //     }
        // })
        return {
            parkName: enterpriseInfo.parkName,
            enterpriseNum,
            productNum
        }
    })
}

// 获取机构列表
export function getServiceList( { type } = {} ) {
    const {service} = require(`@/static/js/service`)
    return type ? service.filter(item => item.type === type) : service
}